package com.zyf.wjy.service;

import com.zyf.wjy.dao.GrowthMonitorDao;
import com.zyf.wjy.entity.GrowthMonitor;
import com.zyf.wjy.util.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 生长监测服务类
 */
@Service
public class GrowthMonitorService {
    
    @Autowired
    private GrowthMonitorDao growthMonitorDao;
    
    /**
     * 查询所有监测记录
     */
    public List<GrowthMonitor> findAll() {
        return growthMonitorDao.findAll();
    }
    
    /**
     * 根据ID查询
     */
    public GrowthMonitor findById(Long id) {
        return growthMonitorDao.findById(id);
    }
    
    /**
     * 根据地块ID查询
     */
    public List<GrowthMonitor> findByFieldId(Long fieldId) {
        return growthMonitorDao.findByFieldId(fieldId);
    }
    
    /**
     * 根据作物ID查询
     */
    public List<GrowthMonitor> findByCropId(Long cropId) {
        return growthMonitorDao.findByCropId(cropId);
    }
    
    /**
     * 保存监测记录
     */
    public GrowthMonitor save(GrowthMonitor monitor) {
        if (monitor.getId() == null) {
            monitor.setMonitorCode(CodeGenerator.generateMonitorCode());
            monitor.setCreateTime(LocalDateTime.now());
        }
        return growthMonitorDao.save(monitor);
    }
    
    /**
     * 删除监测记录
     */
    public boolean deleteById(Long id) {
        return growthMonitorDao.deleteById(id);
    }
}

